//package com.navi.loaddata.loadService.impl.measureData.array;
//
//import com.navi.loaddata.core.errorLog.ErrorLog;
//import com.navi.loaddata.core.etlException.ETLException;
//import com.navi.loaddata.core.gpEntity.array.WppAdefectF;
//import com.navi.loaddata.core.gpEntity.array.WppAdefectGlassF;
//import com.navi.loaddata.core.gpEntity.array.WppAdefectPanelF;
//import com.navi.loaddata.core.mesEntity.MesProducthistory;
//import com.navi.loaddata.core.mysqlEntity.EtlConfD;
//import com.navi.loaddata.core.oracleEntity.array.*;
//import com.navi.loaddata.core.repository.impl.OracleRepositoryImpl;
//import com.navi.loaddata.core.util.LoggerUtil;
//import com.navi.loaddata.core.util.ProcessUtil;
//import com.navi.loaddata.loadService.impl.baseData.array.LoadMesBaseDataServiceARRAY;
//import com.navi.loaddata.loadService.impl.baseData.array.ProcessBaseDataServiceARRAY;
//import org.hibernate.Query;
//import org.hibernate.SQLQuery;
//import org.hibernate.Session;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Map;
//
//import static com.navi.loaddata.core.constant.GenericDef.SHOP_ARY;
//import static java.util.stream.Collectors.groupingBy;
//
///**
// * @author jay
// */
//@Service
//@Transactional(transactionManager = "thirdaryPlatformTransactionManager",
//        rollbackFor = {RuntimeException.class, Exception.class})
//public class LoadMeasureDataServiceARRAY {
//    private static final Logger logger = LoggerFactory.getLogger(LoadMeasureDataServiceARRAY.class);
//    private LoggerUtil loggerUtil = new LoggerUtil(this.getClass());
//
//    @Autowired
//    private OracleRepositoryImpl dao3;
//
//    @Autowired
//    private LoadMesBaseDataServiceARRAY loadMesBaseDataService;
//
//    @Autowired
//    private ProcessBaseDataServiceARRAY processBaseDataService;
//    @Autowired
//    private ProcessDefectDataServiceARRAY processDefectDataService;
//    @Autowired
//    private LoadDefectInfoServiceARRAY loadDefectInfoService;
//
//
//    public int loadMeasureDataMain(EtlConfD etlConfD) throws ETLException {
//        int countSize = 0;
//        Session session = dao3.getEntityManagerSession();
//        try {
//            countSize = loadMeasureData(session, etlConfD);
//            logger.info("LoadMeasureDataMain finish");
//        } catch (ETLException e) {
//            throw new ETLException(e.getMessage());
//        }
//        return countSize;
//    }
//
//    /**
//     * Load Measure Data
//     * <p>
//     * load base -> session table
//     * load defect data  -> session table
//     * call base proc  -> auto commit
//     * call defect proc -> no auto commit
//     * delete defect session table
//     *
//     * @param session
//     * @param etlConfD
//     */
//    private int loadMeasureData(Session session, EtlConfD etlConfD) throws ETLException {
//        int countSize = 0;
//
//
//        // defect
//        List<WppAdefectGlassF> defectGlassList = loadDefectInfoService.loadGlassInfo(etlConfD);
//
//        logger.info("Array defect glass count {}", defectGlassList.size());
//        if (!defectGlassList.isEmpty()) {
//            // base
//            List<EtlArrayStepV> etlStepVList = new ArrayList<>();
//            List<EtlArrayProductV> etlProductVList = new ArrayList<>();
//            try {
//                etlStepVList = loadMesBaseDataService.loadAllFinalStep(SHOP_ARY, ErrorLog.ERROR_PROCESS_ARRAY_STEP_V, logger);
//                etlProductVList = loadMesBaseDataService.loadAllFinalProduct(SHOP_ARY, ErrorLog.ERROR_PROCESS_ARRAY_PRODUCT_V, logger);
//            } catch (Exception e) {
//                logger.error("[{}] - [{}] GET BASE_DATA ERROR", etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp());
//                throw new ETLException(e.getMessage());
//            }
//            // save base
//            try {
//                saveAll(etlStepVList, ErrorLog.ERROR_TABLE_ETL_ARRAY_STEP_V, "BASE_DATA");
//                saveAll(etlProductVList, ErrorLog.ERROR_TABLE_ETL_ARRAY_PRODUCT_V, "BASE_DATA");
//            } catch (Exception e) {
//                logger.error("[{}] - [{}] SAVE BASE_DATA ERROR", etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp());
//                throw new ETLException(e.getMessage());
//            }
//
//            // call base proc
//            try {
//                SQLQuery queryLoadStep = session.createSQLQuery("{call ARRAY_BASE_LOADER.LOAD_STEP()}");
//                queryLoadStep.executeUpdate();
//            } catch (Exception e) {
//                logger.error(e.getMessage());
//                logger.error(LoggerUtil.stackTraceToString(e));
//                logger.error("[{}] - [{}] PROCEDURE LOAD_STEP ERROR", etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp());
//                throw new ETLException("LOAD_STEP:" + ErrorLog.ERROR_LOAD_ARRAY_STEP_V + ":" + e.getMessage());
//            }
//            try {
//                SQLQuery queryLoadProduct = session.createSQLQuery("{call ARRAY_BASE_LOADER.LOAD_PRODUCT()}");
//                queryLoadProduct.executeUpdate();
//            } catch (Exception e) {
//                logger.error(e.getMessage());
//                logger.error(LoggerUtil.stackTraceToString(e));
//                logger.error("[{}] - [{}] PROCEDURE LOAD_PRODUCT ERROR", etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp());
//                throw new ETLException("LOAD_PRODUCT:" + ErrorLog.ERROR_LOAD_ARRAY_PRODUCT_V + ":" + e.getMessage());
//            }
//
//
//            Map<String, List<WppAdefectGlassF>> glassDataTree =
//                    defectGlassList.stream().collect(groupingBy(WppAdefectGlassF::getGlassId));
//            for (Map.Entry<String, List<WppAdefectGlassF>> entry : glassDataTree.entrySet()) {
//                List<WppAdefectGlassF> curGlassList = entry.getValue();
//                //common
//                List<MesProducthistory> cmnGlassVList =
//                        loadMesBaseDataService.loadCommonGlassInfo(etlConfD, curGlassList.get(0).getGlassId());
//                if (cmnGlassVList.size() == 0) {
//                    continue;
//                }
//                List<EtlArrayCmnGlassV> etlCmnGlassVList = new ArrayList<>();
//                try {
//                    etlCmnGlassVList = processBaseDataService.processCommonGlass(cmnGlassVList, ErrorLog.ERROR_PROCESS_ARRAY_CMN_GLASS_V, logger);
//                } catch (Exception e) {
//                    logger.error("[{}] - [{}] PROCESS CMN_GLASS_DATA  ERROR", etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp());
//                    throw new ETLException(curGlassList.get(0).getGlassId() + ":" + e.getMessage());
//                }
//                // save
//                try {
//                    saveAll(etlCmnGlassVList, ErrorLog.ERROR_TABLE_ETL_ARRAY_CMN_GLASS_V, curGlassList.get(0).getGlassId());
//                } catch (Exception e) {
//                    logger.error("[{}] - [{}] SAVE CMN_GLASS_DATA  ERROR", etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp());
//                    throw new ETLException(e.getMessage());
//                }
//                // call procedure
//                try {
//                    SQLQuery queryLoadCmnGlass = session.createSQLQuery("{call ARRAY_MES_LOADER.LOAD_CMN_GLASS()}");
//                    queryLoadCmnGlass.executeUpdate();
//                } catch (Exception e) {
//                    logger.error(LoggerUtil.stackTraceToString(e));
//                    logger.error("[{}] - [{}] PROCEDURE LOAD_CMN_GLASS ERROR", etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp());
//                    throw new ETLException(curGlassList.get(0).getGlassId() + ":" + ErrorLog.ERROR_LOAD_ARRAY_CMN_GLASS_V + ":" + e.getMessage());
//                }
//
//
//                for (int i = 0; i < curGlassList.size(); i++) {
//                    //站点限制
//                    if (true == (ProcessUtil.opeLimit(curGlassList.get(i).getOpeId()))) {
//                        continue;
//                    }
//                    Map<String, String> mOpeDList = loadDefectInfoService.loadMOpeDInfo(etlConfD);
//                    //panel
//                    List<WppAdefectPanelF> defectPanelFEntityList =
//                            loadDefectInfoService.loadPanelInfo(etlConfD, curGlassList.get(i));
//                    logger.info("Array defect panel count {}", defectPanelFEntityList.size());
//                    //defect
//                    List<WppAdefectF> defectFEntityList =
//                            loadDefectInfoService.loadDefectInfo(etlConfD, curGlassList.get(i));
//                    logger.info("Array defect count {}", defectFEntityList.size());
//
//                    List<EtlArrayGlassV> etlArrayGlassVList = new ArrayList<>();
//                    List<EtlArrayChipV> etlArrayChipVList = new ArrayList<>();
//                    List<EtlArrayDefectV> etlArrayDefectVList = new ArrayList<>();
//                    try {
//                        etlArrayGlassVList = processDefectDataService.processGlass(curGlassList.get(i), ErrorLog.ERROR_PROCESS_ARRAY_GLASS_V, logger);
//                        etlArrayChipVList = processDefectDataService.processPanel(defectPanelFEntityList, ErrorLog.ERROR_PROCESS_ARRAY_CHIP_V, logger);
//                        etlArrayDefectVList = processDefectDataService.processDefect(defectFEntityList, mOpeDList, ErrorLog.ERROR_PROCESS_ARRAY_DEFECT_V, logger);
//                    } catch (Exception e) {
//                        logger.error("[{}] - [{}] PROCESS FILE_DATA  ERROR", etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp());
//                        throw new ETLException("PROCESS错误：" + curGlassList.get(i).getGlassId() + ":" + e.getMessage());
//                    }
//                    // save
//                    try {
//                        saveAll(etlArrayGlassVList, ErrorLog.ERROR_TABLE_ETL_ARRAY_GLASS_V, curGlassList.get(i).getGlassId());
//                        saveAll(etlArrayChipVList, ErrorLog.ERROR_TABLE_ETL_ARRAY_CHIP_V, curGlassList.get(i).getGlassId());
//                        saveAll(etlArrayDefectVList, ErrorLog.ERROR_TABLE_ETL_ARRAY_DEFECT_V, curGlassList.get(i).getGlassId());
//                    } catch (Exception e) {
//                        logger.error("[{}] - [{}] SAVE FILE_DATA  ERROR", etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp());
//                        throw new ETLException(e.getMessage());
//                    }
//
//                    // call proc
//                    try {
//                        SQLQuery queryDefect = session.createSQLQuery("{Call ARRAY_FILE_LOADER.LOAD_DATA()}");
//                        queryDefect.executeUpdate();
//                    } catch (Exception e) {
//                        logger.error(ErrorLog.ERROR_LOAD_ARRAY_DATA + ":" + e.getMessage());
//                        logger.error(LoggerUtil.stackTraceToString(e));
//                        logger.error("[{}] - [{}] PROCEDURE FILE_DATA  ERROR", etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp());
//                        throw new ETLException(curGlassList.get(i).getGlassId() + ":" + ErrorLog.ERROR_LOAD_ARRAY_DATA + ":" + e.getMessage());
//                    }
//                    countSize++;
//                    try {
//                        String hqlGlass = "Delete FROM EtlArrayGlassV";
//                        String hqlPanel = "Delete FROM EtlArrayChipV";
//                        String hqlDefect = "Delete FROM EtlArrayDefectV";
//                        Query qGlass = session.createQuery(hqlGlass);
//                        Query qPanel = session.createQuery(hqlPanel);
//                        Query qDefect = session.createQuery(hqlDefect);
//                        qGlass.executeUpdate();
//                        qPanel.executeUpdate();
//                        qDefect.executeUpdate();
//                    } catch (Exception e) {
//                        logger.error(LoggerUtil.stackTraceToString(e));
//                        throw new ETLException("删除DEFECT错误:" + e.getMessage());
//                    }
//                }
//                try {
//                    String hqlCMN = "Delete FROM EtlArrayCmnGlassV";
//                    Query qCMN = session.createQuery(hqlCMN);
//                    qCMN.executeUpdate();
//                } catch (Exception e) {
//                    logger.error(LoggerUtil.stackTraceToString(e));
//                    throw new ETLException("删除CMN错误:" + e.getMessage());
//                }
//            }
//
//        } else {
//            logger.info("没有此区间的数据。"); // todo print log & save etl data
//        }
//        return countSize;
//    }
//
//    public <T> void saveAll(List<T> entities, int errorViewLog, String glassId) throws ETLException {
//        try {
//            dao3.saveAll(entities);
//        } catch (Exception e) {
//            logger.error(errorViewLog + ":" + e.getMessage());
//            logger.error(LoggerUtil.stackTraceToString(e));
//            throw new ETLException(glassId + ":" + errorViewLog + ":" + e.getMessage());
//        }
//    }
//
//}
